<!-- 我的主页 -->
<template>
  <view>
    <PageWarpper page-name="历史订单">
      <template v-slot:main>
        <HistoryTab v-model:tab-index="tabIndex" />
        <view style="margin-top: 136px" v-if="list.length == 0 && currentPage > 1">
          <EmptyCard
            :info="{
              icon: 'empty_icon2',
              iconWidth: '210rpx',
              tip: '暂无订单',
              es: 'No Orders',
              desc: '您还没有此类订单',
            }"
          />
        </view>
        <view class="list">
          <view
            class="list-cell d-flex border-top border-bottom"
            v-for="(item, index) in list"
            :key="index"
          >
            <view
              class="thumb border-right bg-cover"
              v-if="tabIndex == 0"
              :style="{ backgroundImage: 'url(' + item.productPictureUrl + ')' }"
            ></view>
            <OrderInfo
              style="flex: 1"
              :btn-name="getBtnName(item)"
              :item-data="item"
              @btnClick="openOpenPage"
            />
          </view>
        </view>
      </template>
    </PageWarpper>
    <FooterBar :tab-index="2" />
  </view>
</template>

<script setup>
import { ref, watch } from "vue";
import { onReachBottom } from "@dcloudio/uni-app";
import PageWarpper from "@/components/Layer/PageWarpper.vue";
import FooterBar from "@/components/FooterBar/index.vue";
import HistoryTab from "./components/HistoryTab.vue";
import OrderInfo from "@/components/List/OrderInfo.vue";
import EmptyCard from "@/pages/empty/Card.vue";
import { getHistoryOrderList } from "@/server/api.js";
import { onLoad } from "@dcloudio/uni-app";
import { openPage } from "@/util/index.js";

let tabIndex = ref(0);

let list = ref([]);
let currentPage = ref(1);
let isLoad = ref(true);

watch(tabIndex, () => {
  list.value = [];
  currentPage.value = 1;
  isLoad.value = true;
  getList();
});

const openOpenPage = item => {
  let url = "";
  switch (tabIndex.value) {
    case 1:
      url = "/subscribe/pay?id=" + item.id;
      break;
    case 2:
      console.log(item.orderStatus, "orderStatus");
      url = getBtnName(item)
        ? `/subscribe/roompay?page_type=${item.orderStatus == 1 ? 2 : 1}&id=${item.id}&orderNo=${
            item.orderNo
          }`
        : "";
      break;
    default:
      url = "/pages/activity/detail?id=" + item.actId + "&orderNo=" + item.orderNo;
      break;
  }
  openPage(url);
};
let getList = () => {
  getHistoryOrderList({
    current: currentPage.value,
    productType: tabIndex.value + 1,
    size: 10,
  }).then(res => {
    currentPage.value = currentPage.value + 1;
    isLoad.value = !Boolean(res.data.records && res.data.records.length == 0);
    list.value = [...list.value, ...res.data.records];
  });
};

function getBtnName(item) {
  if ([2, 3].includes(item.productType) && ![0, 1].includes(item.orderStatus)) {
    return "";
  }
  return item.orderStatus == 0 ? "去支付" : item.orderStatus == 1 ? "检票入场" : "查看详情";
}

onLoad(options => {
  tabIndex.value = parseInt(options.tabIndex) || 0;
  getList();
});

onReachBottom(() => {
  isLoad.value ? getList() : null;
});
</script>

<style lang="scss" scoped>
#app.content {
  padding-bottom: 0px;
}
.page-name {
  padding-bottom: 40rpx;
}
.list {
  margin-top: 34rpx;
  padding-bottom: 300px;
  .list-cell {
    min-height: 204rpx;
    margin-bottom: 34rpx;
    background-color: #ffffff;
    .thumb {
      width: 154rpx;
      // height: 200rpx;
    }
  }
}
</style>
